import { request } from "@/utils/request";
import type {
  DeleteMenuFunctionParams,
  DeleteParams,
  MenuFunctionParams,
  MenuParams,
  QueryByIdResult,
  QueryMenuFunctionParams,
  QueryMenuFunctionResult,
  QueryParams,
  QueryResult,
  QueryTreeResult,
} from "./typing";
import type { PageResult, ResponseBody } from "@/api/typing";

/**分页查询菜单 */
export const queryByPage = (params: QueryParams) => {
  return request.get<QueryParams, ResponseBody<PageResult<QueryResult>>>(
    "/menu/page",
    {
      params,
    }
  );
};

/**新增菜单 */
export const add = (params: MenuParams) => {
  return request.post<MenuParams, ResponseBody>("/menu/add", params);
};

/**修改菜单 */
export const update = (params: MenuParams) => {
  return request.put<MenuParams, ResponseBody>("/menu/update", params);
};

/**删除菜单 */
export const del = (params: DeleteParams) => {
  return request.delete<DeleteParams, ResponseBody>("/menu/delete", {
    params,
  });
};

/**新增菜单关联功能 */
export const addFunction = (params: MenuFunctionParams) => {
  return request.post<MenuFunctionParams, ResponseBody>(
    "/menu/function/add",
    params
  );
};

/**修改菜单关联功能 */
export const updateFunction = (params: MenuFunctionParams) => {
  return request.put<MenuFunctionParams, ResponseBody>(
    "/menu/function/update",
    params
  );
};

/**列表查询菜单关联功能 */
export const queryFunctionList = (params: QueryMenuFunctionParams) => {
  return request.get<
    QueryMenuFunctionParams,
    ResponseBody<QueryMenuFunctionResult[]>
  >("/menu/function/list", { params });
};

/**删除菜单关联功能 */
export const delFunction = (params: DeleteMenuFunctionParams) => {
  return request.delete<DeleteMenuFunctionParams, ResponseBody>(
    "/menu/function/delete",
    { params }
  );
};

/**菜单树-查询根菜单 */
export const queryRootMenu = () => {
  return request.get<null, ResponseBody<QueryTreeResult[]>>("/menu/root-menu");
};

/**菜单树-查询菜单下的菜单和功能 */
export const queryMF = (menuId: string) => {
  return request.get<string, ResponseBody<QueryTreeResult[]>>(
    "/menu/menu-or-function",
    { params: { menuId } }
  );
};

/**菜单树-查询功能下的权限 */
export const queryPermission = (functionId: string) => {
  return request.get<string, ResponseBody<QueryTreeResult[]>>(
    "/menu/function-permission",
    { params: { functionId } }
  );
};

/**查询单个菜单 */
export const queryById = (menuId: string) => {
  return request.get<string, ResponseBody<QueryByIdResult>>("/menu/id", {
    params: { menuId },
  });
};
